@extends('admin.base')

@section('content')
    <div class="layui-card">
        <blockquote class="layui-elem-quote form-inline layui-form" style="font-size: 14px;padding: 10px">
                <div class="layui-inline" style="margin: 3px 0;">
                    <label class="control-label" for="title" style="font-weight: bold">标题</label>：
                    <div class="layui-input-inline">
                        <input type="text" id="title" class="layui-input" name="title" placeholder="请输入标题">
                    </div>
                </div>
                <div class="layui-inline" style="margin: 3px 0;">
                    <label class="control-label" for="status" style="font-weight: bold">状态</label>：
                    <div class="layui-input-inline">
                        <select id="status">
                            <option value="">全部</option>
                            <option value="1">未开始</option>
                            <option value="2">进行中</option>
                            <option value="3">已结束</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline" style="margin: 3px 0;">
                    <label class="control-label" for="start_time1" style="font-weight: bold">开始时间</label>：
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input"  name="start_time1" id="start_time1">
                    </div>
                    <label class="control-label" for="start_time2">-</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="start_time2" id="start_time2">
                    </div>
                </div>
                <div class="layui-inline" style="margin: 3px 0;">
                    <label class="control-label" for="end_time1" style="font-weight: bold">结束时间</label>：
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="end_time1" id="end_time1">
                    </div>
                    <label class="control-label" for="end_time2">-</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="end_time2" id="end_time2">
                    </div>
                </div>
                <div class="layui-inline" style="margin: 3px 0;">
                    <button id="searchBtn" type="button" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="filter" style="height: 36px;line-height: 36px;">搜索</button>
                    @can('app.open_screen.destroy')
                        <button class="layui-btn layui-btn-sm layui-btn-danger" style="height: 36px;line-height: 36px;" id="listDelete">批量删除</button>
                    @endcan
                    @can('app.open_screen.create')
                        <a class="layui-btn layui-btn-sm" style="height: 36px;line-height: 36px;" href="{{ route('admin.open_screen.create') }}">添 加</a>
                    @endcan
                </div>
        </blockquote>
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <script type="text/html" id="hit_type">
                @{{# if(d.hit_type=='goods_detail'){ }}
                商品详情
                @{{# } if(d.hit_type=='goods_cate'){ }}
                商品分类
                @{{# } if(d.hit_type=='link'){ }}
                连接
                @{{# } if(d.hit_type=='activity'){ }}
                活动
                @{{# } }}
            </script>
            <script type="text/html" id="statusText">
                @{{# if( Date.parse(new Date(d.start_time))  > Date.parse(new Date())  ){ }}
                <span class="layui-badge layui-bg-blue">未开始</span>
                @{{# } if( Date.parse(new Date(d.end_time))  <= Date.parse(new Date())  ){ }}
                <span class="layui-badge">已结束</span>
                @{{# } if( Date.parse(new Date(d.end_time))  > Date.parse(new Date()) && Date.parse(new Date(d.start_time))  <= Date.parse(new Date()) ){ }}
                <span class="layui-badge layui-bg-green">进行中</span>
                @{{# } }}
            </script>
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    @can('app.open_screen.edit')
                        <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
                    @endcan
                    @can('app.open_screen.destroy')
                        <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
                    @endcan
                </div>
            </script>
            <script type="text/html" id="thumb">
                <a href="../storage/@{{d.thumb}}" data-fancybox="rfUploadImg">
                    <img src="../storage/@{{d.thumb}}" alt="" width="28" height="28"/>
                </a>
            </script>
        </div>
    </div>
@endsection

@section('script')
    @can('app.open_screen')
        <script>
            layui.use(['layer','table','form'],function () {
                var layer = layui.layer;
                var form = layui.form;
                var table = layui.table;
                //用户表格初始化

                var dataTable = table.render({
                    elem: '#dataTable'
                    ,height: 'full-200'
                    ,url: "{{ route('admin.open_screen.data') }}" //数据接口
                    ,page: true //开启分页
                    ,limit: 10 //限制条数
                    ,cols: [[ //表头
                        {checkbox: true,fixed: true}
                        ,{field: 'id', title: 'ID',sort: true,width:80}
                        ,{field: 'title', title: '标题',width:140}
                        ,{field: 'hit_type', title: '命中类型',toolbar: '#hit_type',width:100}
                        ,{field: 'hit_content', title: '命中内容',width:120}
                        ,{field: 'thumb', title: '图片',toolbar: '#thumb',width:80}
                        ,{field: 'weight', title: '权重',sort: true,width:80}
                        ,{field: 'second', title: '显示时间',width:100}
                        ,{field: 'start_time', title: '开始时间',sort: true,width:170}
                        ,{field: 'end_time', title: '结束时间',sort: true,width:170}
                        ,{field: 'status', title: '状态',toolbar: '#statusText' ,width:80}
                        ,{field: 'created_at', title: '创建时间',sort: true,width:170}
                        ,{field: 'updated_at', title: '更新时间',sort: true,width:170}
                        ,{fixed: 'right',title: '操作', width: 160, align:'center', toolbar: '#options'}
                    ]]
                });

                //监听工具条
                table.on('tool(dataTable)', function(obj){ //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
                    var data = obj.data //获得当前行数据
                        ,layEvent = obj.event; //获得 lay-event 对应的值
                    if(layEvent === 'del'){
                        layer.confirm('确认删除吗？', function(index){
                            $.post("{{ route('admin.open_screen.destroy') }}",{_method:'delete',ids:[data.id]},function (result) {
                                if (result.code==0){
                                    obj.del(); //删除对应行（tr）的DOM结构
                                    layer.msg(result.msg,{icon:6});
                                }else {
                                    layer.msg(result.msg,{icon:5})
                                }
                                layer.close(index);
                            });
                        });
                    } else if(layEvent === 'edit'){
                        location.href = '/admin/open_screen/'+data.id+'/edit';
                    }
                });

                //按钮批量删除
                $("#listDelete").click(function () {
                    var ids = []
                    var hasCheck = table.checkStatus('dataTable')
                    var hasCheckData = hasCheck.data
                    if (hasCheckData.length>0){
                        $.each(hasCheckData,function (index,element) {
                            ids.push(element.id)
                        })
                    }
                    if (ids.length>0){
                        layer.confirm('确认删除吗？', function(index){
                            $.post("{{ route('admin.open_screen.destroy') }}",{_method:'delete',ids:ids},function (result) {
                                if (result.code==0){
                                    dataTable.reload();
                                    layer.msg(result.msg,{icon:6})
                                }else {
                                    layer.msg(result.msg,{icon:5})
                                }
                                layer.close(index);
                            });
                        })
                    }else {
                        layer.msg('请选择删除项')
                    }
                })
                //搜索
                $("#searchBtn").click(function () {
                    var title = $("#title").val();
                    var status = $("#status").val()
                    var start_time1 = $("#start_time1").val();
                    var start_time2 = $("#start_time2").val()
                    var end_time1 = $("#end_time1").val();
                    var end_time2 = $("#end_time2").val()
                    dataTable.reload({
                        where:{
                            title:title,
                            status:status,
                            start_time1:start_time1,
                            start_time2:start_time2,
                            end_time1:end_time1,
                            end_time2:end_time2
                        }
                    })
                })
            })
            layui.use('laydate', function(){
                var laydate = layui.laydate;
                //日期时间选择器
                laydate.render({
                    elem: '#start_time1'
                    ,type: 'datetime'
                });
                laydate.render({
                    elem: '#start_time2'
                    ,type: 'datetime'
                });
                laydate.render({
                    elem: '#end_time1'
                    ,type: 'datetime'
                });
                laydate.render({
                    elem: '#end_time2'
                    ,type: 'datetime'
                });
            });
        </script>
    @endcan
@endsection